package solution._0250._0257;

import common.TreeNode;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * Created by chen_snow
 * Date: 2021/7/7 23:25
 * Description: 二叉树的所有路径
 */
public class Solution {
    List<String> result = new ArrayList<>();
    Deque<TreeNode> nodeDeque = new ArrayDeque<>();

    public List<String> binaryTreePaths(TreeNode root) {
        findPath(root);
        return null;
    }

    public void findPath(TreeNode root) {
        if (root == null) {
            return;
        }

        nodeDeque.offer(root);

        if (root.left == null && root.right == null) {
            result.add(build(nodeDeque));
        }

        findPath(root.left);
        findPath(root.right);

        nodeDeque.removeLast();
    }

    public String build(Deque<TreeNode> nodeDeque) {
        ArrayList<TreeNode> list = new ArrayList<>(nodeDeque);

        StringBuilder stringBuilder = new StringBuilder();
        for (int index = 0; index < list.size(); index++) {
            stringBuilder.append(list.get(index));

            if (index != list.size() - 1) {
                stringBuilder.append("->");
            }
        }

        return stringBuilder.toString();
    }
}
